package com.treeliked.netdiskdemo.util;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectRequest;
import com.treeliked.netdiskdemo.listener.PutObjectProgressListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * OSS 对象存储工具类
 *
 * @author lqs2
 * @date 2018-12-15, Sat
 */
@Component
public class OssUtils {


    /**
     * oss keyId
     */
    private static String accessKeyId;

    /**
     * oss keySecret
     */
    private static String accessKeySecret;

    /**
     * oss region
     */
    private static String endpoint;

    /**
     * oss bucket name
     */
    private static String bucketName;


    /**
     * 单例client
     **/
    private static class SingletonInstanceClass {
        private static OSSClient client = new OSSClient(endpoint, new DefaultCredentialProvider(accessKeyId, accessKeySecret), new ClientConfiguration());
    }

    public static OSSClient getInstance() {
        return SingletonInstanceClass.client;
    }


    @Value("${oss.accessKeyId}")
    public void setAccessKeyId(String accessKeyId) {
        OssUtils.accessKeyId = accessKeyId;
    }

    @Value("${oss.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        OssUtils.accessKeySecret = accessKeySecret;
    }

    @Value("${oss.endpoint}")
    public void setEndpoint(String endpoint) {
        OssUtils.endpoint = endpoint;
    }

    @Value("${oss.bucketName}")
    public void setBucketName(String bucketName) {
        OssUtils.bucketName = bucketName;
    }

    /**
     * @param filename bucket key
     * @param len      文件大小
     * @param bytes    文件
     */

    public static boolean uploadFile(String filename, long len, byte[] bytes) {

        OSSClient ossClient = getInstance();

        // 创建上传文件请求
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filename, new ByteArrayInputStream(bytes));
        // 进度条
        PutObjectProgressListener progressListener = new PutObjectProgressListener(len);

        putObjectRequest.withProgressListener(progressListener);

        // 执行上传
        ossClient.putObject(putObjectRequest);

        return progressListener.isSucceed();
    }

    /**
     * 获取文件流
     *
     * @param bucketKey object key
     */
    public static InputStream getObjectStream(String bucketKey) {

        OSSClient client = getInstance();
        boolean exist = client.doesObjectExist(bucketName, bucketKey);
        if (exist) {
            OSSObject obj = client.getObject(bucketName, bucketKey);
            return obj.getObjectContent();
        }
        return null;
    }


    /**
     * 删除指定的文件
     *
     * @param ossKey obj key
     */
    public static void dropFile(String ossKey) {
        getInstance().deleteObject(bucketName, ossKey);
    }
}
